﻿$(document).ready(function () {

    $("#fr-LoadUnplannedOrders").submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('body'), false);
        $("#hfwarehouseid").val("");
        $.ajax({
            type: 'POST',
            url: '/load/LoadUnplannedOrders',
            data: serializedForm,
            success: function (data) {
                $(".UnplannedOrderItems").html(data);
                $.ajax({
                    url: "/load/loadUnplannedLoad",
                    success: function (data) {
                        $(".dgvUnplannedLoads").html(data);
                        App.unblockUI($('body'));
                        InitTableSelected();
                    }
                });
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('body'));
            }
        });
        return false;
    });

    $("#fr-loadcreatormovewarehouse").submit(function () {
        var serializedForm = $(this).serialize();
        var loadid = $("#hfloadid").val();
        var oldwarehouseid = $("#WarehouseID").val();
        var orderitem = $(".UnplannedOrderItems table tbody tr.row-selected").attr("data-id");
        App.blockUI($('body'), false);
        $.ajax({
            type: 'POST',
            url: '/load/LoadCreatorMoveWarehouse',
            data: serializedForm + "&loadid=" + loadid + "&oldwarehouseid=" + oldwarehouseid + "&orderitem=" + orderitem,
            success: function (data) {
                if (data != "") {
                    alert(data);
                    $("#MoveWarehouse").modal("hide");
                    validatenewitemstatus(loadid, orderitem);
                }
            },
            error: function () {
                alert('move fail!');
                App.unblockUI($('body'));
            }
        });
        return false;
    });

    $("#fr-searchlistloaddetail").submit(function () {
        var serializedForm = $(this).serialize();
        var datefrom = $("#datefrom").val();
        var specialcondition = $("#specialcondition").val();
        App.blockUI($('body'), false);
        $.ajax({
            type: 'POST',
            url: '/load/searchlistloaddetail',
            data: serializedForm + "&datefrom="+datefrom+"&specialcondition="+specialcondition,
            success: function (data) {
                $(".listloaddetail").html(data);
                App.unblockUI($('body'));
            },
            error: function () {
                alert('search fail!');
                App.unblockUI($('body'));
            }
        });
        return false;
    });

    $('#frUploadImageToLoad').submit(function () {
        var formData = new FormData($("#frUploadImageToLoad")[0]);
        $.ajax({
            url: '/Load/UploadLoadImages',
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                alert("Upload success!");

            },
            error: function (error) {
                alert("Error");
            }
        });
        return false;
    });
    $('#frUploadDeliveryNote').submit(function () {
        var formData = new FormData($("#frUploadDeliveryNote")[0]);
        $.ajax({
            url: '/Load/UploadDeliveryNote?loadid=' + $('#SelectedLoadId').val(),
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                alert("Upload success!");

            },
            error: function (error) {
                alert("Error");
            }
        });
        return false;
    });
    $('#fr-loaddeliverfilter').submit(function () {
        var serializedForm = $(this).serialize();
        App.blockUI($('body'), false);
        $.ajax({
            url: '/Load/LoadDeliverFilter',
            type: 'POST',
            data: serializedForm,
            success: function (data) {
                $('.loaddelivercontent').html(data);
                InitDatatable();
                InitTableSelected();
                App.unblockUI($('body'));

            },
            error: function (error) {
                alert("Error");
                App.unblockUI($('body'));
            }
        });
        return false;
    });

    $('#fr-SendLoadDetail').submit(function () {
        App.blockUI($(".modal-body"));
      
        $.ajax({
            url: "/Load/SendLoadDetail",
            type: "POST",
            data: $(this).serialize(),
            success: function (data) {
                if (data.success == 1) {
                    alert("Success");
                    $("#ViewSendLoadDetail").modal("hide");
                } else {
                    $(".error-message").html(data.message);
                }
                App.unblockUI($(".modal-body"));
            }
        });
        App.unblockUI($(".modal-body"));
        return false;
    });
    
    $('#fr-driverloadsreport').submit(function () {
        App.blockUI($("#div-loadsreport"));
        $.ajax({
            url: "/Load/DriverLoadsReport",
            type: "POST",
            data: $(this).serialize(),
            success: function (data) {
                $("#div-loadsreport").html(data);
                $("#checkreport").val("1");
                App.unblockUI($("#div-loadsreport"));
            },
            error:function() {
                App.unblockUI($("#div-loadsreport"));
            }
        });
        return false;
    });
    
});

function loaddeliverselected(loadid) {
    App.blockUI($('.loaddeliverdetail'), false);
    $.ajax({
        url: "/load/LoadDeliverSelected?loadid=" + loadid,
        success: function (data) {
            $(".loaddeliverdetail").html(data);
            InitDatetimePicker();
            $('#fr-updateloaddeliver').submit(function () {
                if (confirm("Save all these changes?")) {
                    var serializedForm = $(this).serialize();
                    $.ajax({
                        url: '/Load/updateloaddeliver',
                        type: 'POST',
                        data: serializedForm,
                        success: function (data) {
                            if (data.success == 1) {
                                // alert(data.message);
                                $('#fr-loaddeliverfilter').submit();
                            } else {
                                alert(data.message);
                            }
                        },
                        error: function (error) {
                            alert("Error");
                        }
                    });
                }
                return false;
            });
            App.unblockUI($('.loaddeliverdetail'));

        }, error: function () {
            alert('load fail!');
            App.unblockUI($('.loaddeliverdetail'));
        }
    });
}
function loadcreatorloaditem(loadid) {
    App.blockUI($('.dgvLoadItems'), false);
    App.blockUI($('.loadcreatorloaddetail'), false);
    $("#hfloadid").val(loadid);
    $("#hfwarehouseid").val("");
    $(".btn-removeload").attr("onclick", "removeload(" + loadid + ")");

    $.ajax({
        url: "/load/loadcreatorloaditem?loadid=" + loadid,
        success: function (data) {
            $('.dgvLoadItems').html(data);
            App.unblockUI($('.dgvLoadItems'));
            $.ajax({
                url: "/load/LoadCreatorLoadDetail?loadid=" + loadid,
                success: function (data) {
                    $(".loadcreatorloaddetail").html(data);
                    App.unblockUI($('.loadcreatorloaddetail'));
                }, error: function () {
                    alert('load fail!');
                    App.unblockUI($('.loadcreatorloaddetail'));
                }
            });
        }, error: function () {
            alert('load fail!');
            App.unblockUI($('.dgvLoadItems'));
        }
    });
}


function addtoselectedload() {
    var loadid = $("#hfloadid").val();
    if (loadid > 0) {
        var warehouseid = $("#WarehouseID").val();
        var orderitem = $(".UnplannedOrderItems table tbody tr.row-selected").attr("data-id");
        if (orderitem != undefined) {
            App.blockUI($('body'), false);
            $.ajax({
                url: "/load/CheckAlertMoveWarehouse",
                data: { loadid: loadid, warehouseid: warehouseid, orderitem: orderitem },
                success: function (data) {
                    if (data != "") {
                        if (confirm(data)) {
                            checkmovewarehouseorcarrier(loadid, orderitem, warehouseid);
                        }
                    } else {
                        checkmovewarehouseorcarrier(loadid, orderitem, warehouseid);
                    }
                }
            });
        }
    } else {
        alert("Please select or create a load first!");

    }
}

function checkmovewarehouseorcarrier(loadid, orderitem, warehouseid) {
    var carrierid = $("#CarrierID").val();
    $.ajax({
        url: "/load/checkmovewarehouseorcarrier",
        data: { loadid: loadid, warehouseid: warehouseid, orderitem: orderitem, carrierid: carrierid },
        success: function (data) {
            if (data == "warehouse") {
                $("#MoveWarehouse").modal("show");
            } else if (data == "carrier") {
                PopupCenter("/load/movecarrier?warehouseid=" + warehouseid + "&orderitemid=" + orderitem + "&carrierid=" + carrierid + "", 800, 600);
                validatenewitemstatus(loadid, orderitem);
            } else if (data == "") {
                validatenewitemstatus(loadid, orderitem);
            }
        }
    });
}

function validatenewitemstatus(loadid, orderitem) {
    $.ajax({
        url: "/load/ValidateNewItemStatus",
        data: { loadid: loadid, warehouseid: $("#hfwarehouseid").val() },
        success: function (data) {
            if (data == "") {
                $.ajax({
                    url: "/load/LoadCreatorAddToSelectedLoad",
                    type: 'POST',
                    data: { loadid: loadid, warehouseid: $(".newwarehouseid").val(), orderitem: orderitem },
                    success: function (data) {
                        if (data != "") {
                            alert(data);
                        }
                        RefreshLoadUnplannedOrders();
                        loadcreatorloaditem(loadid);
                        App.unblockUI($('body'));
                    }
                });
            } else {
                alert(data);
                App.unblockUI($('body'));
                return false;
            }
            return false;
        }
    });
}

function RefreshLoadUnplannedOrders() {
    $.ajax({
        url: "/load/RefreshLoadUnplannedOrders",
        data: { warehouseid: $("#WarehouseID").val(), carrierid: $("#CarrierID").val() },
        success: function (data) {
            $(".UnplannedOrderItems").html(data);
            $(".btn-movecarrier").removeAttr("onclick");
        }
    });
}

function setmovecarrier(warehouseid, orderitemid, carrierid) {
    $(".btn-movecarrier").attr('onclick', 'PopupCenter("/load/movecarrier?warehouseid=' + warehouseid + '&orderitemid=' + orderitemid + '&carrierid=' + carrierid + '",700,650)');
}

function loadcreatoraddnewload() {
    var warehouseid = $("#WarehouseID").val();
    var carrierid = $("#CarrierID").val();
    App.blockUI($('.dgvUnplannedLoads'), false);
    $.ajax({
        url: "/load/loadcreatoraddnewload",
        data: { warehouseid: warehouseid, carrierid: carrierid },
        success: function (data) {
            $('.dgvUnplannedLoads').html(data);
            InitTableSelected();
        }
    });
}

function refreshload() {
    App.blockUI($('.dgvUnplannedLoads'), false);
    $.ajax({
        url: "/load/LoadUnplannedLoad",
        success: function (data) {
            $('.dgvUnplannedLoads').html(data);
        }
    });
}

//function moveitemselected() {
//    var itemSelect = $(".tbloaditem tbody tr input").is(":checked").length;
//    if (itemSelect == undefined || itemSelect == 0) {
//        alert("No items have been selected");
//    } else {

//    }
//}

function movewarehouseunplanneditem() {
    var movewarehouseid = $("#MoveToWarehouseID").val();
    var orderitem = $(".UnplannedOrderItems table tbody tr.row-selected").attr("data-id");
    var warehouseid = $("#WarehouseID").val();
    var carrierid = $("#CarrierID").val();
    if (orderitem != undefined) {
        App.blockUI($('body'), false);
        $.ajax({
            url: "/load/movewarehouseunplanneditem",
            data: { movewarehouseid: movewarehouseid, orderitem: orderitem, warehouseid: warehouseid, carrierid: carrierid },
            success: function () {
                App.unblockUI($('body'));
            }
        });
    }
}

function ViewBookLoadDetail(loadId, blockui) {
    if (blockui != false) App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/BookLoadDetails",
        data: { loadId: loadId },
        success: function (data) {
            $("#divLoadDetails").html(data);
            InitTableSelected();
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}
function selectedloaditem(loaditemid, orderref, itemstatus, orderid) {
    $("#SelectedOrderRef").html(orderref);
    $("#SelectedOrderStatus").html(itemstatus);
    $("#SelectedOrderId").val(orderid);
    $("#btnConfirmItem").removeAttr("disabled");
    $("#btnDeclineItem").removeAttr("disabled");
}
function declineloaditem() {
    if ($("#SelectedOrderId").val() == 0) {
        alert("Please select item");
        return;
    }
    App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/DeclineLoadItem",
        data: { orderid: $("#SelectedOrderId").val() },
        success: function (data) {
            if (data != "") {
                alert("Error!");
            } else {
                ViewBookLoadDetail($("#LoadId").val(), false);
            }
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}
function confirmloaditem() {
    if ($("#SelectedOrderId").val() == 0) {
        alert("Please select item");
        return;
    }
    App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/ConfirmLoadItem",
        data: { orderid: $("#SelectedOrderId").val() },
        success: function (data) {
            if (data != "") {
                alert("Error!");
            } else {
                ViewBookLoadDetail($("#LoadId").val(), false);
            }
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}
function ConfirmBookLoad() {
    if ($("#LoadId").val() == 0) {
        alert("Please select item");
        return;
    }
    App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/ConfirmBookLoad",
        data: { LoadId: $("#LoadId").val() },
        success: function (data) {
            if (data != "") {
                alert(data);
            } else {
                window.location = window.location;
            }
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}
function ReplanLoad() {
    if ($("#LoadId").val() == 0) {
        alert("Please select item");
        return;
    }
    App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/ReplanLoad",
        data: { LoadId: $("#LoadId").val() },
        success: function (data) {
            if (data != "") {
                alert(data);
            } else {
                window.location = window.location;
            }
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}
function viewloaditems(loadid) {

    App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/BookLoadItems",
        data: { loadId: loadid },
        success: function (data) {
            $("#divViewLoadItems").html(data);
            InitTableSelected();
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}
function ConfirmAllItemAndBookLoad() {
    if ($("#LoadId").val() == 0) {
        alert("Please select item");
        return;
    }
    App.blockUI($("#divLoadDetails"));
    $.ajax({
        url: "/load/ConfirmAllItemAndBookLoad",
        data: { LoadId: $("#LoadId").val() },
        success: function (data) {
            if (data != "") {
                alert(data);
            } else {
                window.location = window.location;
            }
            App.unblockUI($("#divLoadDetails"));
        }, error: function () {
            App.unblockUI($("#divLoadDetails"));
        }
    });
}

function viewloadrefreshsummary() {
    App.blockUI($("body"));
    $.ajax({
        url: "/load/updatesummary",
        success: function (data) {
            $(".TotalUnplannedLoad").html(data.TotalUnplannedLoad);
            $(".TotalReplanLoads").html(data.TotalReplanLoads);
            $(".TotalPlannedLoads").html(data.TotalPlannedLoads);
            $(".TotalBookedInLoads").html(data.TotalBookedInLoads);
            $(".TotalPickedLoads").html(data.TotalPickedLoads);
            $(".TotalDespatchedLoads").html(data.TotalDespatchedLoads);
            App.unblockUI($("body"));
            $("#fr-searchlistloaddetail").submit();
        }
    });
}



function listloaddetailselected(loadid) {
    App.blockUI($(".listloaditemdetail"));
    $.ajax({
        url: "/load/ListLoadItemDetail?loadid=" + loadid,
        success: function (data) {
            $(".listloaditemdetail").html(data);
        }
    });
    $('#SelectedLoadId').val(loadid);
}

function viewdrivermanifest(loadcode) {
    $.ajax({
        url: "/load/viewdrivermanifest?loadcode=" + loadcode
    });
}

function viewdeliverynote(loadcode) {
    $.ajax({
        url: "/load/viewdrivermanifest?loadcode=" + loadcode
    });
}

function viewdeliverynote(loadcode) {
    $.ajax({
        url: "/load/viewboxlabel?loadcode=" + loadcode
    });
}

function viewdespatchlabels(loadcode) {
    $.ajax({
        url: "/load/viewdespatchlabels?loadcode=" + loadcode
    });
}

function viewboxlabels(loadcode) {
    $.ajax({
        url: "/load/viewboxlabels?loadcode=" + loadcode
    });
}

function viewdriverloadacceptancesummary(loadcode) {
    $.ajax({
        url: "/load/viewdriverloadacceptancesummary?loadcode=" + loadcode
    });
}

function unplanload(loadid) {
    App.blockUI($("body"));
    $.ajax({
        url: "/load/unplanload?loadid=" + loadid,
        success: function () {
            viewloadrefreshsummary();
            App.unblockUI($("body"));
        }
    });
}

function ViewLoadItemImages(orderitemid) {
    App.blockUI($("#lstOrderItemImages"), false);
    $.ajax({
        url: "/load/ViewLoadItemImages",
        data: { orderitemid: orderitemid },
        success: function (data) {
            $('#lstOrderItemImages').html(data);
            App.unblockUI($("#lstOrderItemImages"));
        }
    });
    $('#SelectedOrderItemId').val(orderitemid);
}

function DeleteLoadImage(photoid) {
    if (!confirm("Are you sure to remove ?")) {
        return;
    }
    if (photoid == "") {
        alert('Please select image'); return;
    }
    $.ajax({
        url: "/load/DeleteLoadImage",
        data: { photoid: photoid },
        success: function (data) {
            if (data != "") {
                alert(data);
            }
            else $('#lstLoadImage .loadimageitem[data-id="' + photoid + '"]').remove();;
        }
    });
}

function AddImageToSelectedOrder(photoid) {
    var orderitemid = $("#SelectedOrderItemId").val();
    if (orderitemid == "") {
        alert('Please select order'); return;
    }

    $.ajax({
        url: "/Load/AddImageToSelectedOrder",
        data: { orderitemid: orderitemid, photoid: photoid },
        success: function (data) {
            ViewLoadItemImages(orderitemid);
            $('#lstLoadImage .loadimageitem[data-id="' + photoid + '"]').remove();
        }
    });
}
function SetImageIsDriverManifest(photoid, isdrivermanifest) {
    if (photoid == "") {
        alert('Please select image'); return;
    }

    $.ajax({
        url: "/Load/SetImageIsDriverManifest",
        data: { photoid: photoid, isdrivermanifest: isdrivermanifest },
        success: function (data) {
            window.location.reload();
        }
    });
}

function DeleteOrderItemImage(photoid) {
    if (!confirm("Are you sure to remove ?")) {
        return;
    }
    if (photoid == "") {
        alert('Please select image'); return;
    }
    $.ajax({
        url: "/load/DeleteOrderItemImage",
        data: { photoid: photoid },
        success: function (data) {
            if (data != "") {
                alert(data);
            }
            else $('#lstOrderItemImages .loadimageitem[data-id="' + photoid + '"]').remove();;
        }
    });
}
function viewnewdrivermanifest(loadcode) {
    $.ajax({
        url: "/load/viewnewdrivermanifest?loadcode=" + loadcode
    });
}

function viewnewpickingsummary(loadcode) {
    $.ajax({
        url: "/load/viewnewpickingsummary?loadcode=" + loadcode
    });
}

function viewnewdespatchlabels(loadcode) {
    $.ajax({
        url: "/load/viewnewdespatchlabels?loadcode=" + loadcode
    });
}

function viewnewpackinglist(loadcode) {
    $.ajax({
        url: "/load/viewnewpackinglist?loadcode=" + loadcode
    });
}

function viewloadupdatedespatched() {
    App.blockUI($(".listloaditemdetail"));
    $.ajax({
        url: "/load/updatedespatched",
        success: function () {
            App.unblockUI($(".listloaditemdetail"));
        }
    });
}

function deliverydocumentchange(check) {
    if (check.is(":checked")) {
        $(".check5").prop('checked', true);
    } else {
        $(".check5").prop('checked', false);
    }
}
function removeloaditem(loadid, orderitemid) {
    $("#ChooseReason").modal("show");
    $("#hf-orderitemid").val(orderitemid);
    //App.blockUI($("loaditemdetailtable"));
    //$.ajax({
    //    url: "/load/RemoveLoadItem",
    //    data:{loadId:loadid,orderitemid:orderitemid},
    //    success: function () {
    //        alert("Success!");
    //        listloaddetailselected(loadid);
    //        App.unblockUI($("loaditemdetailtable"));
    //    }
    //});
}

function showkybohomedelivery(check) {
    if (check.is(":checked")) {
        $(".ShowDeliveryOrders").show();
        $(".countOrder").html($("#tb-paragonunplanorders").data("countall"));
    } else {
        $(".ShowDeliveryOrders").hide();
        $(".countOrder").html($("#tb-paragonunplanorders").data("countnonedelivery"));
    }
}

function paragonunplanorderselected(orderref, orderid) {
    $(".btn-vieworder").attr("href", "/orderview/orderdetail?orderref=" + orderref);
    $(".btn-selectdeliverydate").attr("onclick", "PopupCenter('/orderview/deliverydate?orderid=" + orderid + "', 1200, 600)");
}

function refreshParagonUnplanOrders() {
    App.blockUI($("body"));
    $.ajax({
        url: "/load/RefreshParagonUnplanOrders",
        success: function (data) {
            $(".paragonUnplanOrderData").html(data);

            $(".countOrder").html($("#tb-paragonunplanorders").data("countnonedelivery"));

            InitTableSelected();
            InitDatatable();
            App.unblockUI($("body"));
        }
    });
}

function SendLoadDetail(loadid) {

    $.ajax({
        url: "/Load/SendLoadDetail?loadid=" + loadid,
        type: "GET",
        success: function (data) {
            $('#SMSDetail').html(data.SMSDetail);
            document.getElementById("sendLoadId").value = data.sendLoadId;
            document.getElementById("Phone").value = data.Phone;
            $(".error-message").html("");
          
        }
    });


}
function SortDate(col) {
    //alert("sort");
    App.blockUI($('.loaddelivercontent'));
    var sort = $("#orderstatus").val();
    var statusid = $("#StatusID").val();
    var driverid = $("#DriverID").val();
    var vehicleid = $("#VehicleID").val();
    var loadcode = $("#LoadCode").val();
    $.ajax({
        url: "/Load/SortDate?statusid="+statusid+"&driverid="+driverid+"&vehicleid="+vehicleid+"&loadcode="+loadcode+"&sort="+sort+"&col=" + col,
        type: "GET",
        success: function (data) {
            $('.loaddelivercontent').html(data);
            InitDatatable();
            InitTableSelected();
            App.unblockUI($('.loaddelivercontent'));
            if (sort == "asc") {
                $("#orderstatus").val("desc");
            } else {
                $("#orderstatus").val("asc");
            }
        },
        error: function (error) {
            alert("Error");
            App.unblockUI($('.loaddelivercontent'));
        }
    });
}
function changeLoadStatus() {
    if ($("#LoadStatusID").val() == "5" || $("#LoadStatusID").val() == "6") {
        $(".box-disabled").show();
    } else {
        $(".box-disabled").hide();
        $("#datefrom").val("");
    }
}
function sortInViewLoad (col) {
    var serializedForm = $("#fr-searchlistloaddetail").serialize();
    var datefrom = $("#datefrom").val();
    var specialcondition = $("#specialcondition").val();
    var sort = $("#sort").val();
    App.blockUI($('.listloaddetail'), false);
    $.ajax({
        type: 'POST',
        url: '/load/searchlistloaddetail',
        data: serializedForm + "&datefrom="+datefrom+"&specialcondition="+specialcondition +"&sort="+sort+"&col="+col,
        success: function (data) {
            $(".listloaddetail").html(data);
            App.unblockUI($('.listloaddetail'));
            if (sort == "asc") {
                $("#sort").val("desc");
            } else {
                $("#sort").val("asc");
            }
        },
        error: function () {
            alert('search fail!');
            App.unblockUI($('.listloaddetail'));
        }
    });
    return false;
}
function checkLoadsReport() {
    var check = $("#checkreport").val();
    if (check == "0") {
        $('#fr-driverloadsreport').submit();
    }
}